package com.example.p2pinvest.common;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.ActivityResultRegistry;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityOptionsCompat;
import androidx.fragment.app.FragmentActivity;

import com.example.p2pinvest.bean.User;
import com.loopj.android.http.AsyncHttpClient;

import butterknife.ButterKnife;

/**
 * @author h2tc01
 * @version 1.0.0
 * 2022/8/12
 * Activity基类
 */
public abstract class BaseActivity extends FragmentActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);

        ActivityManager.getInstance().add(this);

        initTitle();

        initData();
    }

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化页面标题
     */
    protected abstract void initTitle();

    /**
     * 获取布局id以渲染视图
     *
     * @return 布局id
     */
    protected abstract int getLayoutId();

    //启动新的activity
    public void gotoActivity(Class activity, Bundle bundle) {
        Intent intent = new Intent(this, activity);

        if (bundle != null && bundle.size() > 0) {
            intent.putExtra("data", bundle);
        }

        startActivity(intent);
    }

    //销毁当前的activity
    public void removeCurrentActivity() {
        ActivityManager.getInstance().removeCurrent();
    }

    //移除所有的activity
    public void removeAll() {
        ActivityManager.getInstance().removeAll();
    }

    public AsyncHttpClient client = new AsyncHttpClient();

    /**
     * 保存用户信息
     */
    public void saveUser(User user) {
        SharedPreferences user_info = this.getSharedPreferences("user_info", MODE_PRIVATE);
        SharedPreferences.Editor editor = user_info.edit();

        editor.putString("name", user.getName());
        editor.putString("imageurl", user.getImageurl());
        editor.putBoolean("iscredit", user.isCredit());
        editor.putString("phone", user.getPhone());

        editor.apply();
    }

    /**
     * 读取用户信息
     */
    public User readUser() {
        SharedPreferences user_info = this.getSharedPreferences("user_info", MODE_PRIVATE);
        User user = new User();
        user.setName(user_info.getString("name", ""));
        user.setImageurl(user_info.getString("imageurl", ""));
        user.setCredit(user_info.getBoolean("iscredit", false));
        user.setPhone(user_info.getString("phone", ""));

        return user;
    }

    /**
     * 启动页面跳转且包含返回值
     * 有页面跳转且需要处理返回值的子页面可以实现handleResult(ActivityResult result)来完成该方法
     */
    public ActivityResultLauncher<Intent> LaunchActivityForResult() {

        return registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        handleResult(result);
                    }
                }
        );
    }

    /**
     * 子类实现ActivityResultCallback的自定义处理
     * @param result 结果
     */
    protected void handleResult(ActivityResult result){

    }
}
